package com.lili.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lili.controller.request.XasRoleRequest;
import com.lili.controller.request.XasUserRequest;
import com.lili.po.XasPermission;
import com.lili.po.XasRole;
import com.lili.po.XasUser;
import com.lili.service.PermService;
import com.lili.service.UserService;
import com.lili.uitl.ShiroUtils;
import com.lili.vo.MyPage;
import com.lili.vo.ResponseResult;
import com.lili.vo.XasRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("perm")
public class PermController {
    @Autowired
    PermService permService;
    @Autowired
    UserService userService;

    @RequestMapping("xasPermissionList")
    public ResponseResult xasPermissionList() {
        ResponseResult responseResult = new ResponseResult();
        List<XasPermission> xasPermissions = permService.xasPermissionList();
        responseResult.setData(xasPermissions);
        return responseResult;
    }

    @RequestMapping("xasPermissionListByUid")
    public ResponseResult xasPermissionListByUid() {
        ResponseResult responseResult = new ResponseResult();
        String userId = ShiroUtils.getUserId();
        List<XasPermission> xasPermissions = permService.xasPermissionListByUid(Integer.parseInt(userId));
        responseResult.setData(xasPermissions);
        return responseResult;
    }


    @RequestMapping("selectXasRole")
    public ResponseResult selectXasRole(@RequestBody XasRoleRequest xasRoleRequest) {
        ResponseResult responseResult = new ResponseResult();
        MyPage myPage = new MyPage();
        BeanUtils.copyProperties(xasRoleRequest, myPage);
        IPage<XasRole> iPage = permService.selectXasRole(xasRoleRequest);
        myPage.setTotal(iPage.getTotal());
        myPage.setRecords(iPage.getRecords());
        responseResult.setData(myPage);
        return responseResult;
    }

    @RequestMapping("selectXasRoleById")
    public ResponseResult selectXasRoleById(@RequestBody XasRoleRequest xasRoleRequest) {
        ResponseResult responseResult = new ResponseResult();
        XasRoleVo xasRolevo = permService.selectXasRoleById(xasRoleRequest.getId());
        responseResult.setData(xasRolevo);
        return responseResult;
    }

    @RequestMapping("selectAllXasRole")
    public ResponseResult selectXasRole() {
        ResponseResult responseResult = new ResponseResult();
        List<XasRole> xasRoles = permService.selectXasRole();
        responseResult.setData(xasRoles);
        return responseResult;
    }


    @RequestMapping("addXasRole")
    public ResponseResult addXasRole(@RequestBody XasRoleRequest xasRoleRequest) {
        ResponseResult responseResult = new ResponseResult();
        permService.addXasRole(xasRoleRequest);
        return responseResult;
    }

    @RequestMapping("updateXasRole")
    public ResponseResult updateXasRole(@RequestBody XasRoleRequest xasRoleRequest) {
        ResponseResult responseResult = new ResponseResult();
        permService.updateXasRole(xasRoleRequest);
        return responseResult;
    }

    @RequestMapping("delXasRole")
    public ResponseResult delXasRole(@RequestBody XasRole xasRole) {
        ResponseResult responseResult = new ResponseResult();
        permService.delXasRole(xasRole.getId());
        return responseResult;
    }

    @RequestMapping("selectXasUser")
    public ResponseResult selectXasUser(@RequestBody XasUserRequest xasUserRequest) {
        ResponseResult responseResult = new ResponseResult();
        MyPage myPage = new MyPage();
        BeanUtils.copyProperties(xasUserRequest, myPage);
        IPage<XasUser> iPage = userService.selectUser(xasUserRequest);
        myPage.setTotal(iPage.getTotal());
        myPage.setRecords(iPage.getRecords());
        responseResult.setData(myPage);
        return responseResult;
    }

    @RequestMapping("addXasUser")
    public ResponseResult addXasUser(@RequestBody XasUser xasUser) {
        ResponseResult responseResult = new ResponseResult();
        SimpleHash password = new SimpleHash("MD5", xasUser.getPassword());
        xasUser.setPassword(password.toString());
        userService.addXasUser(xasUser);
        return responseResult;
    }

    @RequestMapping("updateXasUser")
    public ResponseResult updateXasUser(@RequestBody XasUser xasUser) {

        ResponseResult responseResult = new ResponseResult();
        if (StringUtils.isNotBlank(xasUser.getPassword())) {
            SimpleHash password = new SimpleHash("MD5", xasUser.getPassword());
            xasUser.setPassword(password.toString());
        }
        userService.updateXasUser(xasUser);
        return responseResult;
    }

    @RequestMapping("delXasUser")
    public ResponseResult delXasUser(@RequestBody XasUser xasUser) {
        ResponseResult responseResult = new ResponseResult();
        userService.delXasUser(xasUser.getUid());
        return responseResult;
    }

}
